package com.apes.auto.maintain.dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

/**
 * @program: apes-server
 * @description
 * @author: czs
 * @create: 2021-10-20 14:39
 **/
@Component
public class MaintainPeriodAppDao extends ApesDao {

    public JSONArray findByCXID(JSONObject param){

        // 获取基础油数据
        String sSQL_JCYLX = "SELECT CXID PZCXID, JCYLX, GG JYGG FROM BYZQ_APP@DB_SHUJERP WHERE PJMCID = 'A00003' AND CXID= :PZCXID";

        return toJSONArray(find(sSQL_JCYLX, param));
    }

    /***
     * 公共获取基础油类型（PZCXID JYGG JCYLX）
     * @param param
     * @throws Exception
     */
    public JSONArray getJCYLXCommon(JSONObject param) {
        String sSQL_EXT = " SELECT * FROM BYZQ_DZB@DB_SHUJERP WHERE JCYLX=:JCYLX AND GG=:JYGG ";
        JSONArray returnArray = toJSONArray(find(sSQL_EXT, param));

        String sSQL_PJYL = " SELECT PJYL FROM BYZQ_APP@DB_SHUJERP WHERE PJMCID='A00003' AND CXID=:PZCXID ";
        JSONArray returnPJYL =  toJSONArray(find(sSQL_PJYL, param));

        //获取车型用油增加波箱油接头型号、用量、油品型号信息
        String sSQL_CXPJDZML = " SELECT NVL(A.OEID,'-') OEID,A.PJMCID,NVL(A.HYSL,'-') HYSL FROM MAS_CXPJDZML_1@DB_SHUJERP A WHERE A.PJMCID IN ('000668','000728') AND A.PPMC = 'PDK' AND A.YXBJ = '1' AND PZCXID = :PZCXID ";
        JSONArray cxArray =  toJSONArray(find(sSQL_CXPJDZML, param));

        String sInterface = "";  //接口
        String sOilType = ""; //油型号
        String sOilConsumption = ""; //油用量

        if(cxArray.size()!=0){
            for(int i =0;i<cxArray.size();i++){
                JSONObject json = cxArray.getJSONObject(i);
                if("000728".equals(json.getString("PJMCID"))){ //jiekou
                    if("".equals(sInterface)){
                        sInterface = json.getString("OEID") ;
                    }else{
                        sInterface = sInterface + ";" + json.getString("OEID") ;
                    }

                }else {  //you
                    if("".equals(sOilType)){
                        sOilType =  json.getString("OEID") ;
                        sOilConsumption =  json.getString("HYSL") ;
                    }else{
                        sOilType = sOilType + ";" + json.getString("OEID") ;
                        sOilConsumption = sOilConsumption + ";" + json.getString("HYSL") ;
                    }

                }
            }
        }
        JSONObject cxJson = new JSONObject();
        cxJson.put("JTXH",sInterface);
        cxJson.put("YPXH",sOilType);
        cxJson.put("YPYL",sOilConsumption);
        if(returnArray.size() == 0){
            returnArray.add(cxJson);
        }else if(returnArray.size()!=0 && returnPJYL.size()!=0){
            returnArray.stream().forEach((item)->{
                JSONObject tempJson = (JSONObject)item;
                tempJson.put("PJYL",returnPJYL.getJSONObject(0).getString("PJYL"));
                tempJson.put("JTXH",cxJson.getString("JTXH"));
                tempJson.put("YPXH",cxJson.getString("YPXH"));
                tempJson.put("YPYL",cxJson.getString("YPYL"));
            });
        }
        return returnArray;
    }

}
